from fastapi import FastAPI,Request
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

# 配置CORS (允许跨域请求)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 生产环境应更严格
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {"message": "Hello from your PC server!"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

async def main():

a = await read_item(1, "hello")
print(a)

@app.get("/req/")
async def req(request: Request):
    print("req")
    request_method = request.method
    request_url = request.url
    print(f"{request_method}: {request_url}")
